<html>
<head>
  <title>montecarlo.py</title>
</head>
<body bgcolor="#ffffff" text="#000000">
<pre>
<font color="#008000">&quot;&quot;</font><font color="#008000">&quot;
This example approxiamates PI using a monte carlo simulation.
&quot;</font><font color="#008000">&quot;&quot;</font>

<strong>import</strong> <font color="#2040a0">mpi</font>
<strong>import</strong> <font color="#2040a0">random</font>

<font color="#2040a0">def</font> <font color="#2040a0">computePi</font><font color="4444FF">(</font> <font color="#2040a0">size</font>, <font color="#2040a0">nsamples</font><font color="4444FF">)</font><font color="4444FF">:</font>
    <font color="#2040a0">oldpi</font>, <font color="#2040a0">pi</font>, <font color="#2040a0">mypi</font>,<font color="#2040a0">pisum</font> <font color="4444FF">=</font> <font color="#FF0000">0.0</font>,<font color="#FF0000">0.0</font>,<font color="#FF0000">0.0</font>,<font color="#FF0000">0.0</font>
    <font color="#2040a0">done</font> <font color="4444FF">=</font> <font color="#2040a0">False</font>
    
    <font color="#2040a0">inside</font> <font color="4444FF">=</font> <font color="#FF0000">0</font>
    <font color="#444444"># Monte Carlo bit</font>
    <strong>for</strong> <font color="#2040a0">i</font> <strong>in</strong> <font color="#2040a0">xrange</font><font color="4444FF">(</font><font color="#2040a0">nsamples</font><font color="4444FF">)</font><font color="4444FF">:</font>
        <font color="#2040a0">x</font> <font color="4444FF">=</font> <font color="#2040a0">random</font>.<font color="#2040a0">random</font><font color="4444FF">(</font><font color="4444FF">)</font>
        <font color="#2040a0">y</font> <font color="4444FF">=</font> <font color="#2040a0">random</font>.<font color="#2040a0">random</font><font color="4444FF">(</font><font color="4444FF">)</font>
        <strong>if</strong> <font color="4444FF">(</font><font color="4444FF">(</font><font color="#2040a0">x</font><font color="4444FF">*</font><font color="#2040a0">x</font><font color="4444FF">)</font><font color="4444FF">+</font><font color="4444FF">(</font><font color="#2040a0">y</font><font color="4444FF">*</font><font color="#2040a0">y</font><font color="4444FF">)</font><font color="4444FF">&lt;</font><font color="#FF0000">1</font><font color="4444FF">)</font><font color="4444FF">:</font>
            <font color="#2040a0">inside</font><font color="4444FF">+</font><font color="4444FF">=</font><font color="#FF0000">1</font>
    <font color="#444444"># </font>
    <font color="#2040a0">sum_inside</font> <font color="4444FF">=</font> <font color="#2040a0">mpi</font>.<font color="#2040a0">allreduce</font><font color="4444FF">(</font><font color="#2040a0">inside</font>, <font color="#FF0000">1</font>, <font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_INT</font>, <font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_SUM</font>, <font color="#2040a0">mpi</font>.<font color="#2040a0">MPI_COMM_WORLD</font><font color="4444FF">)</font> 
    <font color="#444444"># The &quot;* 4&quot; is needed because we're computing the number of points inside</font>
    <font color="#444444"># a QUARTER unit circle.  So we're really computing (PI / 4).</font>
    <font color="#2040a0">pi</font> <font color="4444FF">=</font> <font color="4444FF">(</font> <font color="#2040a0">sum_inside</font><font color="4444FF">[</font><font color="#FF0000">0</font><font color="4444FF">]</font> / <font color="4444FF">(</font><font color="#2040a0">nsamples</font><font color="4444FF">*</font><font color="#2040a0">size</font><font color="4444FF">*</font><font color="#FF0000">1.0</font><font color="4444FF">)</font> <font color="4444FF">)</font> <font color="4444FF">*</font> <font color="#FF0000">4</font>
    <strong>return</strong> <font color="#2040a0">pi</font>

<strong>if</strong> <font color="#2040a0">__name__</font><font color="4444FF">=</font><font color="4444FF">=</font><font color="#008000">&quot;__main__&quot;</font><font color="4444FF">:</font>
    <font color="#2040a0">rank</font>, <font color="#2040a0">size</font> <font color="4444FF">=</font> <font color="#2040a0">mpi</font>.<font color="#2040a0">init</font><font color="4444FF">(</font><font color="4444FF">)</font>
    <font color="#444444"># More sample points should make a more accurate value for pi.</font>
    <font color="#2040a0">pi</font> <font color="4444FF">=</font> <font color="#2040a0">computePi</font><font color="4444FF">(</font> <font color="#2040a0">size</font>, <font color="#FF0000">10000</font> <font color="4444FF">)</font>
    <strong>if</strong><font color="4444FF">(</font><font color="#2040a0">rank</font><font color="4444FF">=</font><font color="4444FF">=</font><font color="#FF0000">0</font><font color="4444FF">)</font><font color="4444FF">:</font>
        <strong>print</strong> <font color="#008000">&quot;Computed value of pi on&quot;</font>,<font color="#2040a0">size</font>,<font color="#008000">&quot;processors is&quot;</font>,<font color="#2040a0">pi</font>
    <font color="#2040a0">mpi</font>.<font color="#2040a0">finalize</font><font color="4444FF">(</font><font color="4444FF">)</font>


</pre>
<hr>
syntax highlighted by <a href="http://www.palfrader.org/code2html">Code2HTML</a>, v. 0.9.1
</body>
</html>
